import { msgType } from '../../utils/tools';
import { conversionAddress } from '../../utils/qqMap';
// QQMapKey是在腾讯地图开放平台设置的密钥
const QQMAPKEY = "EP4BZ-XUGKZ-YN5XA-ZEQAV-X7ETH-VLBKA";

Page({
  data: {
    name: "index2 page",
    isLogin: false,
    userInfo: {},
    phoneNum: "151",
    location: {
      longitude: 0,
      latitude: 0
    }
  },
  onLoad() {
    wx.getUserInfo({
      success: (res) => {
        // var userInfo = res.userInfo; // 获取用户信息对象
        // var avatarUrl = userInfo.avatarUrl; // 头像地址
        // console.log('res', res);
        if (res.userInfo) {
          this.setData({isLogin: true, userInfo: res.userInfo})
        }
      },
    });
    // 初始化地理信息
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log('res 获取地理位置', res);
        if (msgType(res)) {
          // this.getAddress(res.latitude, res.longitude)
          this.setData({
            location: {longitude: res.longitude, latitude: res.latitude}
          })
        }
      }
    });
  },
  onShow() {
    this.getStorage("token");
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
      });
    }
  },

  // 用户登录
  userLogin() {
    // console.log("用户登录");
    wx.login({
      success: (res) => {
        console.log(res);
        wx.showToast({
          title: "登录信息" + res.code,
          icon: "success",
          duration: 2000,
        });
        // console.log('登录信息！！！', res, res.code)
        // if (res.code) {
        //   this.setStorage('token', res.code);
        // }
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      },
    });
  },

  getStorage(key: string) {
    if (!key) return;
    wx.getStorage({
      key: key,
      encrypt: true,
      success(res) {
        console.log("getStorage ===---=== 成功", res, res.data);
        // wx.showToast({
        //   title: "成功" + res.data,
        //   icon: "success",
        //   duration: 2000,
        // });
      },
    });
  },

  // 获取手机号
  getPhoneNumber(e: any) {
    console.log(e);
    console.log(this.data.phoneNum);
  },
  clearPhoneNum() {
    this.setData({ phoneNum: "" });
  },

  // 获取地理位置
  getLocation() {
    // 微信API，获取经纬度
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        console.log('res 获取地理位置', res);
        if (msgType(res)) {
          // this.getAddress(res.latitude, res.longitude)
          conversionAddress(res.latitude, res.longitude)
        }
      }
    });
  },
  getAddress(lat: number, lon: number) {
    wx.request({
      // 腾讯地图转换接口
      url: "https://apis.map.qq.com/ws/geocoder/v1",
      data: {
        key: QQMAPKEY,
        location: `${lat},${lon}`
      },
      success(res) {
        // console.log('逆转换后的地址', res);
        const { data } = res;
        const { message, result } = data as any;
        if (message === "query ok") {
          const { address, address_component } = result;
          const { nation, province, city, district, street, street_number } =
            address_component;
          const nowAddress = `${nation}-${city}-${province}-${district}-${street}-${street_number} --- ${address} `;
          wx.showModal({
            title: "转换后的地理",
            content: nowAddress,
          });
        }
      },
    });
  }
});
